/**
 * 
 */
package com.alkandros.linesmash.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.alkandros.linesmash.R;
import com.alkandros.linesmash.adapter.ExpandableFoodAdapter;
import com.alkandros.linesmash.location.ILocationCallBack;
import com.alkandros.linesmash.models.RestaurantMenuModel;
import com.alkandros.linesmash.models.RestaurantMenuItem;
import com.alkandros.linesmash.models.Restaurant;

/**
 * @author Jiju Induchoodan
 * 
 */
public class OrderMenuSelection extends BaseActivity implements
		OnClickListener, ILocationCallBack {

	ExpandableFoodAdapter listAdapter;
	ExpandableListView expListView;
	List<String> listDataHeader;
	HashMap<String, List<RestaurantMenuItem>> listDataChild;
	private RestaurantMenuItem foodType;
	private RestaurantMenuModel food;
	private TextView checkout;
	private TextView addMore;
	private TextView subtotal;

	private Restaurant restaurantModelReceived;
	ArrayList<RestaurantMenuItem> foodItemArray;
	private Intent intent;
	private RestaurantMenuItem restaurantMenuItem;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.restaurant_item_selection);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			restaurantModelReceived = (Restaurant) extras
					.getSerializable("model");
			restaurantMenuItem = (RestaurantMenuItem) extras
					.getSerializable("restuarant_item");

			/*
			 * ActionBar actionBar = getSupportActionBar();
			 * actionBar.setDisplayHomeAsUpEnabled(true);
			 * actionBar.setDisplayShowHomeEnabled(false);
			 * actionBar.setDisplayShowTitleEnabled(true);
			 * actionBar.setDisplayUseLogoEnabled(false);
			 * actionBar.setTitle(restaurantModelReceived.getRestaurant());
			 */
		}

		setHeading(restaurantModelReceived.getTitle());
		setLeftButton();
		setRightButton();
		setCartButton();
		checkout = (TextView) findViewById(R.id.right_button);
		addMore = (TextView) findViewById(R.id.addmore);
		subtotal = (TextView) findViewById(R.id.textSubtotal);
		expListView = (ExpandableListView) findViewById(R.id.list);
		prepareListData();
		listAdapter = new ExpandableFoodAdapter(this, listDataHeader,
				listDataChild);
		expListView.setAdapter(listAdapter);
		checkout.setOnClickListener(this);
		addMore.setOnClickListener(this);
		expListView.setGroupIndicator(null);

		if (restaurantMenuItem != null) {
			subtotal.setText("$ " + restaurantMenuItem.getPrice());
		}

	}

	/*
	 * Preparing the list data
	 */
	private void prepareListData() {
		food = new RestaurantMenuModel();
		listDataHeader = new ArrayList<String>();
		foodItemArray = new ArrayList<RestaurantMenuItem>();
		listDataChild = new HashMap<String, List<RestaurantMenuItem>>();

		// Adding child data
		listDataHeader.add("Size");
		listDataHeader.add("Boosters");

		// Adding child data
		List<String> sizeItems = new ArrayList<String>();
		sizeItems.add("Small");
		sizeItems.add("Regular");
		sizeItems.add("Large");
		for (int i = 0; i < 3; i++) {
			foodType = new RestaurantMenuItem();
			foodType.setName(sizeItems.get(i));
			foodType.setPrice("+$0.49");
			foodType.setType("size");
			foodItemArray.add(foodType);
		}
		listDataChild.put(listDataHeader.get(0), foodItemArray);

		foodItemArray = new ArrayList<RestaurantMenuItem>();
		List<String> boosters = new ArrayList<String>();
		boosters.add("Protein Powder");
		boosters.add("Green Tea");
		boosters.add("Weight Loss");
		for (int i = 0; i < 3; i++) {
			foodType = new RestaurantMenuItem();
			foodType.setName(boosters.get(i));
			foodType.setPrice("+$0.49");
			foodType.setType("boosters");
			foodItemArray.add(foodType);
		}
		listDataChild.put(listDataHeader.get(1), foodItemArray);
	}

	@Override
	public void onClick(View v) {
		if (v == checkout) {
			intent = new Intent(OrderMenuSelection.this, Checkout.class);
			startActivityForResult(intent, CHECKOUT_PAGE);
		} else {
			finish();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==DINING_LOCATION_MENU)
		{
			setResult(DINING_LOCATION_MENU);
			finish();
		}
	}

	@Override
	public void setResult(String address) {
		// TODO Auto-generated method stub

	}
}